package com.service.scheduling.service.impl;

import com.service.dao.constants.ConfigConstants;
import com.service.dao.model.Member;
import com.service.dao.model.MemberOut;
import com.service.dao.model.SysConfig;
import com.service.dao.po.MemberCache;
import com.service.scheduling.service.*;
import com.service.scheduling.support.MemberCacheUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年11月15日</dd>
 * </dl>
 *
 * @author
 */

@Slf4j
@Service
public class MemberCacheServiceImpl implements MemberCacheService {

    @Autowired
    private MemberService memberService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private MemberJAService memberJAService;
    @Autowired
    private MemberCacheUtils memberCacheUtils;
    @Autowired
    private MemberOutService memberOutService;
    @Autowired
    private OrderProductOtherService orderProductOtherService;

    @Override
    public MemberCache initMemberPerformance(Integer memberId) {
        MemberCache.MemberCacheBuilder builder = MemberCache.builder();
        //设置会员基本信息
        Member member = memberService.getById(memberId);
        builder.memberId(memberId);
        builder.memberAccount(member.getUserAccount());
        builder.memberGrade(member.getVipGrade());

        //获取会员所有的出局总和
        MemberOut memberOut = memberOutService.getLastMemberOutRecord(memberId);
        //设置会员总消费额：购买礼包消费
        BigDecimal totalConsumption = getMemberTotalConsumption(memberId, memberOut);
        builder.totalConsumption(totalConsumption);

        //是否为有效会员
        SysConfig configRep = configService.getByConfigName(ConfigConstants.VALID_MEMBER_CONSUMPTION);
        BigDecimal validMemberLimit = BigDecimal.valueOf(Long.parseLong(configRep.getConfValue()));
        builder.validMember(totalConsumption.compareTo(validMemberLimit) >= 0);
        //设置奖金
        memberJAService.getMemberIncome(memberId, builder, memberOut);

        List<Member> memberList = memberService.listByPid(memberId);
        List<Integer> validMemberIdList = memberList.parallelStream().filter(item -> {
            BigDecimal totalMoney = getMemberTotalConsumption(item.getId(),
                    memberOutService.getLastMemberOutRecord(item.getId()));
            return totalMoney.compareTo(validMemberLimit) > 0;
        }).map(Member::getId).collect(Collectors.toList());
        builder.validDRUserIdList(validMemberIdList);

        return builder.build();
    }

    private BigDecimal getMemberTotalConsumption(Integer memberId, MemberOut memberOut) {
        if (!ObjectUtils.isEmpty(memberOut)) {
            return orderProductOtherService.getGiftOrderTotalMoneyByTime(memberId, memberOut.getCreateTime(), new Date());
        } else {
            return orderProductOtherService.getGiftOrderTotalMoney(memberId);
        }
    }

    @Override
    public MemberCache getMemberCache(Integer memberId) {
        MemberCache memberCache = memberCacheUtils.getMemberCache(memberId);
        if (ObjectUtils.isEmpty(memberCache)) {
            log.debug("getMemberCache->会员<{}>信息无缓存，获取原数据 - start", memberId);
            memberCache = initMemberPerformance(memberId);
            memberCacheUtils.setMemberCache(memberCache);
            log.debug("getMemberCache->会员<{}>信息无缓存，获取原数据 - end", memberId);
        }
        return memberCache;
    }

    @Override
    public void setMemberCache(MemberCache memberCache) {
        memberCacheUtils.setMemberCache(memberCache);
    }

    @Override
    public BigDecimal getSomedayGiftOrderTotalPayMoney(Integer memberId, Date someday) {
        BigDecimal resultNum = memberCacheUtils.getSomedayGiftOrderTotalMoney(memberId, someday);
        if (ObjectUtils.isEmpty(resultNum) || resultNum.compareTo(BigDecimal.ZERO) <= 0) {
            resultNum = orderProductOtherService.getSomedayGiftOrderTotalMoney(memberId, someday);
        }
        return resultNum;
    }

    @Override
    public void removeSomedayGiftOrderTotalPayMoney(Integer memberId, Date someday) {
        memberCacheUtils.removeSomedayGiftOrderTotalMoney(memberId, someday);
    }

}
